package by.brest.bspc.repository.converters;

import by.brest.bspc.domain.Grade;
import by.brest.bspc.domain.Student;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;

import java.util.LinkedList;
import java.util.List;

import static by.brest.bspc.repository.Constants.CLASS_NUMBER;
import static by.brest.bspc.repository.Constants.DIRESPECT;
import static by.brest.bspc.repository.Constants.DISCIPLINE_NAME;
import static by.brest.bspc.repository.Constants.FACULTY;
import static by.brest.bspc.repository.Constants.GRADE_LIST;
import static by.brest.bspc.repository.Constants.GRADE_TYPE;
import static by.brest.bspc.repository.Constants.GROUP_NAME;
import static by.brest.bspc.repository.Constants.MARK;
import static by.brest.bspc.repository.Constants.RESPECT;
import static by.brest.bspc.repository.Constants.SEMESTER_NUMBER;
import static by.brest.bspc.repository.Constants.SPECIALTY;
import static by.brest.bspc.repository.Constants.SPECIALTY_CODE;
import static by.brest.bspc.repository.Constants.SPECIALTY_NAME;
import static by.brest.bspc.repository.Constants.STATUS;
import static by.brest.bspc.repository.Constants.ID;
import static by.brest.bspc.repository.Constants.STUDENT_NAME;
import static by.brest.bspc.repository.Constants.STUDY;
import static by.brest.bspc.repository.Constants.TEACHER_NAME;

/**
 * This class converts <code>Student</code> to <code>DBObject</code>.
 *
 * @date: 06.09.13 19:38
 * @author: Natallia
 */
public class StudentWriteConverter implements Converter<Student, DBObject> {
    private static final Logger LOG = LoggerFactory.getLogger(StudentWriteConverter.class);

    /**
     * Converts <code>Student</code> to <code>DBObject</code>.
     *
     * @param student <code>Student</code>, that need to convert to <code>DBObject</code>.
     * @return <code>DBObject</code>.
     */
    @Override
    public DBObject convert(Student student) {
        LOG.debug(">> Student, that need to convert: " + student);

        DBObject studentDBObject = new BasicDBObject();

        studentDBObject.put(ID, new ObjectId (student.getId()));
        studentDBObject.put(STUDENT_NAME, student.getName());
        studentDBObject.put(STUDY, student.getStudy());
        studentDBObject.put(FACULTY, student.getFaculty());

        DBObject specialtyDBObject = new BasicDBObject();
        specialtyDBObject.put(SPECIALTY_NAME, student.getSpecialty().getName());
        specialtyDBObject.put(SPECIALTY_CODE, student.getSpecialty().getCode());

        studentDBObject.put(SPECIALTY, specialtyDBObject);
        studentDBObject.put(CLASS_NUMBER, student.getClassNumber());
        studentDBObject.put(GROUP_NAME, student.getGroupName());

        List<DBObject> gradeListDBObject = new LinkedList<>();
        for (Grade grade : student.getGradeList()) {
            DBObject gradeDBObject = new BasicDBObject();
            gradeDBObject.put(GRADE_TYPE, grade.getGradeType());
            gradeDBObject.put(SEMESTER_NUMBER, grade.getSemesterNumber());
            gradeDBObject.put(DISCIPLINE_NAME, grade.getDisciplineName());
            gradeDBObject.put(TEACHER_NAME, grade.getTeacherName());
            gradeDBObject.put(MARK, grade.getMark());
            gradeDBObject.put(RESPECT, grade.getRespect());
            gradeDBObject.put(DIRESPECT, grade.getDirespect());

            LOG.debug(">> Converted gradeDBObject: " + gradeDBObject);

            gradeListDBObject.add(gradeDBObject);
        }

        LOG.debug(">> Converted gradeListDBObject: " + gradeListDBObject);

        studentDBObject.put(GRADE_LIST, gradeListDBObject);
        studentDBObject.put(STATUS, student.getStatus());

        LOG.debug(">> Converted studentDBObject: " + studentDBObject);

        return studentDBObject;
    }
}
